/**
 * Copyright (C) 2012 Eduard Sedov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.es.eventbus.annotation;

import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.es.eventbus.EventBus;

/**
 *
 * @author ESEDO
 */
public class AnnotationProcessor {

    public static void subscribe(Object obj) {

        if (obj == null) {
            return;
        }

        Method[] methods = obj.getClass().getMethods();

        for (Method method : methods) {

            EventAnnotation eventAnnotation = method.getAnnotation(EventAnnotation.class);

            if (method.getParameterTypes().length == 1 && eventAnnotation != null) {

                Class eventClass = eventAnnotation.value();

                if (eventClass == null) {
                    eventClass = method.getParameterTypes()[0];
                } else if (!method.getParameterTypes()[0].isAssignableFrom(eventClass)) {
                    throw new RuntimeException("Cannot subscribe the ");
                }

                if (method.isAccessible()) {
                    final Method m = method;
                    AccessController.doPrivileged(new PrivilegedAction() {
                        public Object run() {
                            m.setAccessible(true);
                            return null;
                        }
                    });
                }
                ProxyEventListener proxy = new ProxyEventListener(obj, method);

                EventBus.get().subscribe(eventClass, proxy);
            }

            TopicAnnotation topicAnnotation = method.getAnnotation(TopicAnnotation.class);

            if (topicAnnotation != null
                    && (method.getParameterTypes().length == 1
                    || (method.getParameterTypes().length == 2 && method.getParameterTypes()[0] == String.class))) {

                String topic = topicAnnotation.value();

                if (method.isAccessible()) {
                    final Method m = method;
                    AccessController.doPrivileged(new PrivilegedAction() {
                        public Object run() {
                            m.setAccessible(true);
                            return null;
                        }
                    });
                }
                ProxyTopicListener proxy = new ProxyTopicListener(obj, method);

                EventBus.get().subscribe(topic, proxy);
            }
        }
    }

    public static void unsubscribe(Object obj) {
    }
}
